﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using Lexalytics;

namespace SalienceEngineDemo.Dialogs
{
    public partial class CustomProfileDialog : Form
    {
        private SalienceDemo parent;

        public CustomProfileDialog(SalienceDemo parent)
        {
            this.parent = parent;
            InitializeComponent();
        }

        #region Form UI event handlers
        /// <summary>
        /// When profile name text changes, check if we can enable the Create button
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void txtProfileName_TextChanged(object sender, EventArgs e)
        {
            btnCreate.Enabled = ValidateForm();
        }

        /// <summary>
        /// Handler for browsing for data directory
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void btnBrowseDataDir_Click(object sender, EventArgs e)
        {
            FolderBrowserDialog dialog = new FolderBrowserDialog();
            dialog.ShowNewFolderButton = false;
            if (dialog.ShowDialog() == System.Windows.Forms.DialogResult.OK)
            {
                this.txtDataDirPath.Text = dialog.SelectedPath;
                if (this.chkLockDir.Checked)
                {
                    this.txtUserDirPath.Text = dialog.SelectedPath + "\\user";
                }
            }
            btnCreate.Enabled = ValidateForm();
        }

        /// <summary>
        /// Handler for browsing for user directory
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void btnBrowseUserDir_Click(object sender, EventArgs e)
        {
            FolderBrowserDialog dialog = new FolderBrowserDialog();
            dialog.ShowNewFolderButton = false;
            if (dialog.ShowDialog() == System.Windows.Forms.DialogResult.OK)
            {
                this.txtUserDirPath.Text = dialog.SelectedPath;
            }
            btnCreate.Enabled = ValidateForm();
        }

        /// <summary>
        /// Handler for toggle that locks user directory to data directory
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void chkLockDir_CheckedChanged(object sender, EventArgs e)
        {
            if (chkLockDir.Checked)
            {
                this.btnBrowseUserDir.Enabled = false;
                if (System.IO.Directory.Exists(this.txtDataDirPath.Text + "\\user"))
                {
                    this.txtUserDirPath.Text = this.txtDataDirPath.Text + "\\user";
                }
            }
            else
            {
                this.btnBrowseUserDir.Enabled = true;
            }
            btnCreate.Enabled = ValidateForm();
        }

        #endregion

        #region Form button event handlers
        /// <summary>
        /// Create button handler, performs task of validating profile components and creating the profile for use. Sets dialog result to OK on successful
        /// profile creation for main app to handle appropriately.
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void btnCreate_Click(object sender, EventArgs e)
        {
            if (this.txtDataDirPath.Text == String.Empty && this.txtProfileName.Text == String.Empty)
            {
                this.Close();
            }
            else if (System.IO.Directory.Exists(this.txtDataDirPath.Text))
            {
                if (this.txtProfileName.Text != String.Empty)
                {
                    bool ProfileNameExists = SalienceDemo.oConfig.UserProfileExists(this.txtProfileName.Text);
                    if (!ProfileNameExists)
                    {

                        Settings sToAdd = new Settings(this.txtProfileName.Text, this.txtDataDirPath.Text, SalienceDemo.oConfig.sInstallRoot, this.chkShortMode.Checked, this.txtUserDirPath.Text);

                        for (int i = SalienceDemo.oConfig.nNonCustomProfiles; i < parent.cmbModeSwitch.Items.Count; i++)
                        {
                            if (i == parent.cmbModeSwitch.Items.Count - 1)
                            {
                                try
                                {
                                    SalienceFive tempEngine = new SalienceFive(Properties.Settings.Default.LicensePath, this.txtDataDirPath.Text, this.txtUserDirPath.Text);
                                }
                                catch (SalienceException)
                                {
                                    MessageBox.Show("Invalid data/user directory. Please try another.", "Salience Demo", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
                                    return;
                                }
                                SalienceDemo.oConfig.userSettings.Insert(i, sToAdd);
                                parent.cmbModeSwitch.Items.Insert(i, this.txtProfileName.Text);
                                parent.cmbModeSwitch.SelectedIndex = i;
                                Properties.Settings.Default.nMode = i;
                                Properties.Settings.Default.Save();
                                break;
                            }
                            else if (Char.ConvertToUtf32(SalienceDemo.oConfig.userSettings[i].settings["Name"].ToLower(), 0) > Char.ConvertToUtf32(this.txtProfileName.Text.ToLower(), 0))
                            {
                                try
                                {
                                    SalienceFive tempEngine = new SalienceFive(Properties.Settings.Default.LicensePath, this.txtDataDirPath.Text, this.txtUserDirPath.Text);
                                }
                                catch (SalienceException)
                                {
                                    MessageBox.Show("Invalid data/user directory. Please try another.", "Salience Demo", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
                                    return;
                                }
                                SalienceDemo.oConfig.userSettings.Insert(i, sToAdd);
                                parent.cmbModeSwitch.Items.Insert(i, this.txtProfileName.Text);
                                parent.cmbModeSwitch.SelectedIndex = i;
                                Properties.Settings.Default.nMode = i;
                                Properties.Settings.Default.Save();
                                break;
                            }
                        }
                        SalienceDemo.oConfig.activeSettings().Save();
                        this.DialogResult = System.Windows.Forms.DialogResult.OK;
                        this.Close();
                    }
                    else if (ProfileNameExists)
                    {
                        MessageBox.Show("This profile name is already used. Please choose another.", "Salience Demo", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
                    }
                }
                else
                {
                    MessageBox.Show("You have not entered a name for this custom profile.", "Salience Demo", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);

                }
            }
            else if (!System.IO.Directory.Exists(this.txtDataDirPath.Text))
            {
                MessageBox.Show("Cannot find the specifiled directory. Please check your path.", "Salience Demo", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
            }
        }

        /// <summary>
        /// Cancel button handler. Sets dialog result to Cancel for main app to handle appropriately.
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void btnCancel_Click(object sender, EventArgs e)
        {
            this.DialogResult = System.Windows.Forms.DialogResult.Cancel;
            this.Close();
        }

        #endregion

        #region Utility methods
        /// <summary>
        /// Simple validation that all fields needed for profile have been set, does not guarantee their validity.
        /// </summary>
        /// <returns></returns>
        private bool ValidateForm()
        {
            bool bRet = false;
            if (txtProfileName.Text.Length > 0 && txtDataDirPath.Text.Length > 0 && txtUserDirPath.Text.Length > 0)
                bRet = true;
            return bRet;
        }
        #endregion

    }
}
